﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Security.Cryptography;

namespace CatSentenceAnalyzer
{
    interface ILangSegment
    {
        //string GetTdbNextWord();
        int GetSenLength();
        int GetDiectoryIndexKey(); //TDB = 1 首字， = 2整字 分词
        int GetSentenceIndexKey();
        int GetSentenceSumKey();
        ArrayList GetMatchCollection3();
        ArrayList GetMatchCollection2();
        ArrayList GetMatchCollection1();
        void GetSegment(int segmenttype);
    }
    public  class CLangSegment : ILangSegment
    {
        public MatchCollection m_sentencecoll;
        public string m_otxt;
        public const int MINLEN = 3;
        public ArrayList m_MatchList;
        public ArrayList m_MatchSumKey;
        public int m_type, m_iCount = 0;
        public int SumKeyWordLength;
        //private int m_segmenttype;

        //virtual public int GetSenLength() { return 0; }
        //virtual public int GetDiectoryIndexKey() { return 0; }
        virtual public int GetSentenceSumKey() { return 0; }
        virtual public ArrayList GetMatchCollection3() { return null; }
        virtual public ArrayList GetMatchCollection2() { return null; }
        virtual public ArrayList GetMatchCollection1() { return null; }
        virtual public ArrayList GetTdbCollection() { return null; }
        virtual public int GetDiectoryIndexKey() { return 0; }
        static public int TMSALLINDEXTYPE =0, TDBALLINDEXTYPE = 1, TDBSEEKINDEXTYPE = 2;
        static public int MININDEXTYPE = 3, SUMKEYINDEXTYPE =4, MAXINDEXTYPE = 7;
        static public int TDBCOLLTYPE = 10;
        virtual public int GetSenLength()
        {
            if (m_sentencecoll == null)
                GetRegexCollection();
            return m_sentencecoll.Count;
        }
        
        virtual public int GetSentenceIndexKey()
        {
            string regx = @"[\s]";
            string tmp = ""; int sum = 0;
            tmp = Regex.Replace(m_otxt, regx, "");
            regx = GetHasBase64(tmp);
            GetSumKey(tmp);
            putHashKey(regx);
            return sum;
        }

        virtual public void GetSegment( int segmenttype )
        {
            //m_segmenttype = segmenttype;
            if (otxt.Length == 0) return;
            if (segmenttype == TMSALLINDEXTYPE)
            {
                GetSentenceIndexKey();//Hash 唯一索引 
                GetSentenceSumKey();//查找相似性 100%
                GetMatchCollection1();//查找标点 分隔单句100%，其中一句
                GetMatchCollection2();//查找1/2句
                GetMatchCollection3();//查找1/3句
            }
            //
            if (segmenttype == TDBALLINDEXTYPE) 
            {
                GetDiectoryIndexKey(); //给TDB 首字索引
                GetSentenceIndexKey();
            }
            if (segmenttype == TDBSEEKINDEXTYPE) 
            {
                GetDiectoryIndexKey();
            }
            if (segmenttype == TDBCOLLTYPE)
            {
                GetTdbCollection();
            }
            if (segmenttype == MININDEXTYPE)
                GetSentenceIndexKey();
            if (segmenttype == SUMKEYINDEXTYPE)
                GetSentenceSumKey();
            if (segmenttype == 5)
                GetMatchCollection1();
            if(segmenttype == 6)
                GetMatchCollection2();
            if (segmenttype == MAXINDEXTYPE)
                GetMatchCollection3();
        }
        public void CATLongSeg(){
            m_MatchList = new ArrayList();
            m_MatchSumKey = new ArrayList();
        }
        public string TypeRegx
        {
            get{
                string s=""; 
                switch (m_type)
                {
                    case 0: s = @"[\s]"; break;
                    case 1: s = @"[a-z\s]+"; break;//英文
                    case 2: s = @"[\u4e00-\u9fa5]+"; break;//中文
                }
                return s;
            }
        }

        public string otxt
        {
            get
            {
                return m_otxt;
            }
            set
            {
                m_otxt = value;
            }
        }

        protected void putHashKey( string key)
        {
            if (key == "0") return;
            if (!m_MatchSumKey.Contains(key))
            {
                m_MatchSumKey.Add(key);
            }
        }
        protected void putKey(int sum)
        {
            if (sum>0)
            {
                putHashKey(sum.ToString());
            }
        }

        protected int GetSumKey(string val)
        {
            if(val=="")  return 0;
            if (m_MatchSumKey == null) m_MatchSumKey = new ArrayList();
            if (m_MatchList == null) m_MatchList = new ArrayList();
            if (val.Length>0 && !m_MatchList.Contains(val)) m_MatchList.Add(val);
            else return 0;
            return SumKey(val);
        }

        public static int SumKey(string val)
        {
            int sum = 0; char[] chars;
            int imax = 0, imin =0;
            chars = val.ToLower().ToCharArray();
            for (int j = 0; j < chars.Length; j++)
            {
                imax = Math.Max(imax, (int)chars[j]);
                if (j == 0) imin = imax;
                imin = Math.Min(imin, (int)chars[j]);
                sum += chars[j];
            }
            if (chars.Length >0 )
            {
                sum = sum * 10 + sum + imax + imin;
            }
            return sum;
        }

        protected void GetRegexCollection(string regx)
        {
            if (regx.Length == 0) regx = @"[\u4e00-\u9fa5]+";// @"[a-z]+|\d+|[\u4e00-\u9fa5]+"; //中文引擎只要中文 不要英文， 比较方便处理
            Regex reg;
            reg = new Regex(regx, RegexOptions.Singleline | RegexOptions.IgnoreCase);
            m_sentencecoll = reg.Matches(otxt);
        }

        protected void GetRegexCollection()
        {
            GetRegexCollection(TypeRegx);
        }

        protected MatchCollection GetRegx(string regx, string stxt)
        {
            if (regx.Length == 0) return null;
            Regex reg;
            reg = new Regex(regx, RegexOptions.Singleline | RegexOptions.IgnoreCase);
            return reg.Matches(stxt);
        }

        static private string GetHasBase64(string sval)
        {
            byte[] newv = new byte[sval.Length];
            try
            {
                SHA1 sha = new SHA1CryptoServiceProvider();
                newv = sha.ComputeHash(Encoding.UTF8.GetBytes(sval));
                return Convert.ToBase64String(newv);
            }
            catch (System.Exception )
            {
                return ""; 
            }
        }
    }
}
